package top.drinyeit.weilong.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.util.PermissionsChecker;
import top.drinyeit.weilong.util.SPUtil;

/**
 * Created by bigHead on 2017/11/20.
 */

public class SplashActivity extends AppCompatActivity {

    private boolean isFinished;
    protected PermissionsChecker mPermissionsChecker; // 权限检测器
    private static final int PERMISSON_REQUESTCODE = 0;

    /**
     * 需要进行检测的权限数组
     */
    protected String[] needPermissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_splash);
            //延迟2S跳转
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }, 500);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

//    /**
//     * 获取权限集中需要申请权限的列表
//     *
//     * @param permissions
//     * @return
//     * @since 2.5.0
//     */
//    private List<String> findDeniedPermissions(String[] permissions) {
//        List<String> needRequestPermissonList = new ArrayList<String>();
//        for (String perm : permissions) {
//            if (ContextCompat.checkSelfPermission(this,
//                    perm) != PackageManager.PERMISSION_GRANTED
//                    || ActivityCompat.shouldShowRequestPermissionRationale(
//                    this, perm)) {
//                needRequestPermissonList.add(perm);
//            }
//        }
//        return needRequestPermissonList;
//    }
//
//    /**
//     * @param
//     * @since 2.5.0
//     */
//    private void checkPermissions(String... permissions) {
//        List<String> needRequestPermissonList = findDeniedPermissions(permissions);
//        if (null != needRequestPermissonList
//                && needRequestPermissonList.size() > 0) {
//            ActivityCompat.requestPermissions(this,
//                    needRequestPermissonList.toArray(
//                            new String[needRequestPermissonList.size()]),
//                    PERMISSON_REQUESTCODE);
//        }
//    }
//
//    /**
//     * 检测是否说有的权限都已经授权
//     *
//     * @param grantResults
//     * @return
//     * @since 2.5.0
//     */
//    private boolean verifyPermissions(int[] grantResults) {
//        for (int result : grantResults) {
//            if (result != PackageManager.PERMISSION_GRANTED) {
//                return false;
//            }
//        }
//        return true;
//    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode,
//                                           String[] permissions, int[] paramArrayOfInt) {
//        if (requestCode == PERMISSON_REQUESTCODE) {
//            if (!verifyPermissions(paramArrayOfInt)) {
//                showMissingPermissionDialog();
//            }else {
//                SPUtil.setShareBoolean(Constant.IS_NEED_CHECK,true);
//                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
//                startActivity(intent);
//                finish();
//            }
//        }
//    }
//
//    /**
//     * 显示提示信息
//     *
//     * @since 2.5.0
//     */
//    private void showMissingPermissionDialog() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle(R.string.notifyTitle);
//        builder.setMessage(R.string.notifyMsg);
//
//        // 拒绝, 退出应用
//        builder.setNegativeButton(R.string.cancel,
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        finish();
//                    }
//                });
//
//        builder.setPositiveButton(R.string.setting,
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        startAppSettings();
//                    }
//                });
//
//        builder.setCancelable(false);
//
//        builder.show();
//    }
//
//    /**
//     * 启动应用的设置
//     *
//     * @since 2.5.0
//     */
//    private void startAppSettings() {
//        Intent intent = new Intent(
//                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//        intent.setData(Uri.parse("package:" + getPackageName()));
//        startActivity(intent);
//    }



}
